package com.yzx.lifeassistants.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.flyco.animation.BaseAnimatorSet;
import com.flyco.animation.BounceEnter.BounceTopEnter;
import com.flyco.animation.SlideExit.SlideBottomExit;
import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.NormalListDialog;
import com.haarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.yzx.lifeassistants.R;
import com.yzx.lifeassistants.adapter.MyDialogAdapter;
import com.yzx.lifeassistants.adapter.PartTimeAdapter;
import com.yzx.lifeassistants.base.BaseActivity;
import com.yzx.lifeassistants.bean.PartTimeInfo;
import com.yzx.lifeassistants.model.IQueryPartTime;
import com.yzx.lifeassistants.model.callback.IQueryPartTimeCB;
import com.yzx.lifeassistants.model.impl.QueryPartTimeImpl;
import com.yzx.lifeassistants.utils.ToastUtils;
import com.yzx.lifeassistants.view.widget.CircularLoadingDialog;

/**
 * @Description: 兼职资讯
 * @author: yzx
 * @time: 2016-3-7 下午1:27:01
 */
public class PartTimeActivity extends BaseActivity implements OnClickListener,
		OnItemClickListener {
	private ImageButton backBtn;// 返回按钮
	private TextView titleTV;// 标题
	private ImageButton moreBtn;// 更多按钮
	private ListView partTimeLV;// 兼职列表
	private PullToRefreshListView partTimePTRLV;// 刷新
	private ILoadingLayout partTimeILL;// 加载

	private NormalListDialog placeDialog;// 城市弹框
	private List<String> placeList;// 城市列表
	private String place;
	private Handler handler;
	private IQueryPartTimeCB queryCallBack;// 回调
	private IQueryPartTime queryActivity;// 查询
	private CircularLoadingDialog dialog;// 加载
	private static final int STATE_REFRESH = 0;// 下拉刷新
	private static final int STATE_MORE = 1;// 加载更多
	private int curPage = 0; // 当前页的编号，从0开始
	private int loadType;// 加载类型
	private List<PartTimeInfo> dataList;// 图书列表
	private PartTimeAdapter adapter;// 通知列表适配器

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_part_time);
		initView();
		initDialog();
		setListener();
		initData();
	}

	/**
	 * 
	 * @Description: 初始化控件
	 */
	private void initView() {
		backBtn = (ImageButton) findViewById(R.id.part_time_back_btn);
		titleTV = (TextView) findViewById(R.id.part_time_title_tv);
		moreBtn = (ImageButton) findViewById(R.id.part_time_more_btn);
		partTimePTRLV = (PullToRefreshListView) findViewById(R.id.part_time_listview);
		partTimeILL = partTimePTRLV.getLoadingLayoutProxy();
		partTimeILL.setLastUpdatedLabel("");
		partTimeILL.setPullLabel("继续拖动");
		partTimeILL.setRefreshingLabel("正在装载数据···");
		partTimeILL.setReleaseLabel("放开装载更多");
		partTimeLV = partTimePTRLV.getRefreshableView();
	}

	/**
	 * 
	 * @Description: 初始化弹框
	 */
	private void initDialog() {
		BaseAnimatorSet mBasIn = new BounceTopEnter();
		BaseAnimatorSet mBasOut = new SlideBottomExit();
		placeList = new ArrayList<String>();
		placeList.add("全市");
		placeList.add("闽侯");
		placeList.add("鼓楼");
		placeList.add("台江");
		placeList.add("仓山");
		placeList.add("马尾");
		placeList.add("晋安");
		MyDialogAdapter adapter = new MyDialogAdapter(this, placeList);
		placeDialog = new NormalListDialog(this, adapter);
		placeDialog.title("请选择兼职地点")//
				.showAnim(mBasIn)//
				.dismissAnim(mBasOut);//
		placeDialog.setOnOperItemClickL(new OnOperItemClickL() {
			@Override
			public void onOperItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				placeDialog.dismiss();
				String city = placeList.get(position);
				if ("全市".equals(city)) {
					titleTV.setText("福州兼职");
				} else {
					titleTV.setText(city + "兼职");
				}
				switch (position) {
				case 0: {// 全市
					place = "job";
					break;
				}
				case 1: {// 闽侯
					place = "3127";
					break;
				}
				case 2: {// 鼓楼
					place = "381";
					break;
				}
				case 3: {// 台江
					place = "383";
					break;
				}
				case 4: {// 仓山
					place = "385";
					break;
				}
				case 5: {// 马尾
					place = "388";
					break;
				}
				case 6: {// 晋安
					place = "392";
					break;
				}
				default:
					break;
				}
				queryPartTime(0, STATE_REFRESH);
			}
		});
	}

	/**
	 * 
	 * @Description: 设置监听
	 */
	private void setListener() {
		backBtn.setOnClickListener(this);
		moreBtn.setOnClickListener(this);
		// 滑动监听
		partTimePTRLV.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				if (firstVisibleItem == 0) {
					partTimeILL.setLastUpdatedLabel("");
					partTimeILL.setPullLabel("下拉刷新");
					partTimeILL.setRefreshingLabel("加载中···");
					partTimeILL.setReleaseLabel("释放刷新");
				} else if (firstVisibleItem + visibleItemCount == totalItemCount) {
					partTimeILL.setLastUpdatedLabel("");
					partTimeILL.setPullLabel("继续拖动");
					partTimeILL.setRefreshingLabel("正在装载数据···");
					partTimeILL.setReleaseLabel("放开装载更多");
				}
			}
		});
		// 下拉刷新监听
		partTimePTRLV.setOnRefreshListener(new OnRefreshListener2<ListView>() {

			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				// 下拉刷新(从第一页开始装载数据)
				queryPartTime(0, STATE_REFRESH);
			}

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				// 上拉加载更多(加载下一页数据)
				queryPartTime(curPage, STATE_MORE);
			}

		});
		partTimeLV.setOnItemClickListener(this);
	}

	/**
	 * 
	 * @Description: 初始化数据
	 */
	private void initData() {
		place = "job";
		dialog = new CircularLoadingDialog(this);
		dataList = new ArrayList<PartTimeInfo>();
		adapter = new PartTimeAdapter(this, dataList);
		SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(
				adapter);
		swingBottomInAnimationAdapter.setAbsListView(partTimeLV);
		partTimeLV.setAdapter(swingBottomInAnimationAdapter);
		adapter.notifyDataSetChanged();
		handler = new Handler();
		queryCallBack = new IQueryPartTimeCB() {

			@Override
			public void querySuccess(final List<PartTimeInfo> infos) {
				handler.post(new Runnable() {

					@Override
					public void run() {// 查询成功
						partTimePTRLV.onRefreshComplete();
						dialog.dismiss();
						if (STATE_REFRESH == loadType) {
							dataList.clear();
							curPage = 0;
							partTimeLV.smoothScrollToPosition(0);
						}
						dataList.addAll(infos);
						adapter.notifyDataSetChanged();
						curPage++;
					}
				});
			}

			@Override
			public void queryError(final int code) {
				handler.post(new Runnable() {

					@Override
					public void run() {
						dialog.dismiss();
						partTimePTRLV.onRefreshComplete();
						switch (code) {
						case IQueryPartTimeCB.RESULR_NULL: {// 暂无更多
							if (STATE_MORE == loadType) {
								showToast("暂时还未有更多的兼职信息~", true);
							}
							if (STATE_REFRESH == loadType) {
								showToast("暂时还未有兼职信息~", true);
							}
							break;
						}
						case IQueryPartTimeCB.QUERY_ERROR: {// 获取失败
							showToast("获取失败，请确认网络连接后刷新重试~", false);
							break;
						}
						default:
							break;
						}
						adapter.notifyDataSetChanged();
					}

				});
			}
		};
		queryActivity = new QueryPartTimeImpl(this, queryCallBack);
		queryPartTime(0, STATE_REFRESH);
	}

	/**
	 * 
	 * @Description: 查询教务处通知
	 */
	private void queryPartTime(int page, int actionType) {
		loadType = actionType;
		dialog.show();
		queryActivity.queryPartTime(page + 1, place);
	}

	/**
	 * 
	 * @Description: 按键监听
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.part_time_back_btn: {// 返回
			finish();
			break;
		}
		case R.id.part_time_more_btn: {// 更多
			placeDialog.show();
			placeDialog.getWindow().setGravity(Gravity.CENTER);
			break;
		}
		default:
			break;
		}
	}

	/**
	 * 
	 * @Description: 列表按键监听
	 */
	@Override
	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long arg3) {
		Intent intent = new Intent(this, PartTimeInfoActivity.class);
		intent.putExtra("PART_TIME_INFO_URL", dataList.get(position - 1)
				.getUrl());
		startActivity(intent);
	}

	/**
	 * 
	 * @Description: Toast显示信息
	 */
	private void showToast(String message, Boolean isInfo) {
		if (isInfo) {
			ToastUtils.showInfoToast(this, message, R.id.part_time_top_ll);
		} else {
			ToastUtils.showAlertToast(this, message, R.id.part_time_top_ll);
		}

	}
}
